package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.annotation.LogAnnotation;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.dto.volunteer.VolunteerDto;
import com.csun.cmny.provider.model.dto.volunteer.VolunteerModifyPwdDto;
import com.csun.cmny.provider.model.dto.volunteer.VolunteerQueryDto;
import com.csun.cmny.provider.model.vo.VolunteerInforVo;
import com.csun.cmny.provider.model.vo.VolunteerVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacVolunteerService;
import com.csun.cmny.provider.validator.UacVolunteerAddValidator;
import com.csun.cmny.provider.validator.UacVolunteerEditValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-05-23 15:22
 * @Description: the class uac volunteer controller
 */
@RestController
@RequestMapping(value = "/volunteer", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacVolunteerController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private UacVolunteerService uacVolunteerService;


    /**
     * 志愿者添加
     *
     * @param volunteerDto  volunteer dto
     * @param br            binding result
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/add")
    public Wrapper addVolunteer(@Validated(UacVolunteerAddValidator.class)
                                    @RequestBody VolunteerDto volunteerDto,
                                BindingResult br) {

        volunteerDto.setGroupId(getLoginAuthDto().getGroupId());
        Integer result = uacVolunteerService.addVolunteer(volunteerDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 志愿者修改
     *
     * @param volunteerDto  volunteer dto
     * @param br            binding result
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/edit")
    public Wrapper editVolunteer(@Validated(UacVolunteerEditValidator.class)
                                     @RequestBody VolunteerDto volunteerDto,
                                 BindingResult br) {

        Integer result = uacVolunteerService.updateVolunteer(volunteerDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 志愿者删除
     *
     * @param volunteerId   volunteer id
     *
     * @return  the wrapper.
     */
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/deleteById/{volunteerId}")
    public Wrapper deleteVolunteerById(@PathVariable Long volunteerId) {

        // 校验志愿者Id是否为空
        Preconditions.checkArgument(volunteerId != null, cmnyMessageSource
                .getMessage("volunteer.id.null", null));

        Integer result = uacVolunteerService.deleteVolunteerById(volunteerId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 志愿者分页查询
     *
     * @param volunteerQueryDto volunteer query dto
     *
     * @return  the wrapper.
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryVolunteerListWithPage(@RequestBody VolunteerQueryDto volunteerQueryDto) {
        volunteerQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = uacVolunteerService.queryVolunteerListWithPage(volunteerQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), pageInfo);
    }

    @PostMapping(value = "/queryList")
    public Wrapper queryVolunteerList() {
        PageInfo pageInfo = uacVolunteerService.queryVolunteerList(getLoginAuthDto().getGroupId());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), pageInfo);
    }

    /**
     * 志愿者个人信息修改
     *
     * @param volunteerDto  volunteer dto
     * @param br            binding result
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/personalEdit")
    public Wrapper personalEditVolunteer(@Validated(UacVolunteerEditValidator.class)
                                         @RequestBody VolunteerDto volunteerDto,
                                         BindingResult br) {
        //校验修改的志愿者是否是本人
        VolunteerVo volunteerVO = uacVolunteerService.getVolunteerByVolunteerId(volunteerDto.getId());
        if(Long.parseLong(volunteerVO.getUserId()) != getLoginAuthDto().getUserId()){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        Integer result = uacVolunteerService.updateVolunteer(volunteerDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 志愿者个人信息查看
     *
     * @param userId  volunteer dto
     * @return  the wrapper.
     */
  //  @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getById/{userId}")
    public Wrapper getById ( @PathVariable Long userId ){

        // 校验志愿者Id是否为空
        Preconditions.checkArgument(userId != null, cmnyMessageSource
                .getMessage("volunteer.id.null", null));
        VolunteerVo volunteerVo = uacVolunteerService.getVolunteerByUserId(userId);
/*         UacUser user = uacUserService.findUserInfoByUserId(userId);
       //当身份为志愿者时
        if(user.getUserCode() == "volunteer") {
            //校验查看详情的志愿者是否是本人
            if (userId != getLoginAuthDto().getUserId()) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                        .getMessage("operation.failed", null));
            }
        }*/
        if(volunteerVo == null){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("volunteer.id.null", null));
        }
        //return WrapMapper.ok(volunteerVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), volunteerVo);
    }

   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/modifyPwd")
    public Wrapper modifyVolunteerPwd(@RequestBody VolunteerModifyPwdDto volunteerModifyPwdDto) {

        Integer result = uacVolunteerService.modifyVolunteerPwd(volunteerModifyPwdDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /*
    * 大数据平台志愿者服务查询
    * */
    @PostMapping("/selectVolunteerByNurseHome")
    public Wrapper selectVolunteerByNurseHome(@RequestBody VolunteerInforVo vo){
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        vo.setGroupId(loginAuthDto.getGroupId());
        List<VolunteerInforVo> volunteerVos = uacVolunteerService.selectVolunteerByNurseHome(vo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), volunteerVos);
    }

}
